
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:请求验证码（服务端V3版）
 *
 * @author jerryliu
 * @since 2015年7月24日 上午10:28:20
 */
public class RequestValidateCodeTaskV3 {

    public RequestVerifiyCodeResponseV3 request(String username, int smstype, String mobile)
            throws AppException {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        RequestVerifiyCodeResponseV3 respObj = new RequestVerifiyCodeResponseV3();
        ResponsePackage resp = new RequestVerifiyCodeResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("cmd", ProtocolCmd.CMD_SEND_VALIDATE_CODE_PHONE);
        params.put("username", username);
        params.put("smstype", smstype);
        params.put("mobile", mobile);
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (AppException e) {
            throw e;
        }
    }

    static class RequestVerifiyCodeResponsePackage extends
            HttpBaseResponsePackage<RequestVerifiyCodeResponseV3> {

        @Override
        protected void handleResponse(RequestVerifiyCodeResponseV3 resetPasswordResponse,
                String jsonStr) {
            try {
                LogUtil.d("response", "RequestValidateCodeTaskV3 = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    resetPasswordResponse.setCode(code);
                    resetPasswordResponse.setMessage(message);
                    resetPasswordResponse.setPrompt(prompt);

                    if (resetPasswordResponse.isUseful()
                            && ProtocolCmd.CMD_SEND_VALIDATE_CODE_PHONE.equals(cmd)) {
                        JSONObject o = jsonResp.getJSONObject("data");
                        resetPasswordResponse.setUnixTime(o.getString("unixtime"));
                        resetPasswordResponse.setToken(o.getString("verifytoken"));
                        resetPasswordResponse.setOk(true);
                    }
                }
            } catch (Exception e) {
            }
        }

    }

    /**
     * 响应消息
     */
    public static class RequestVerifiyCodeResponseV3 extends ResponseMessage {

        private String unixTime;

        private String token;

        public String getUnixTime() {
            return unixTime;
        }

        public void setUnixTime(String unixTime) {
            this.unixTime = unixTime;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }
}
